import {AfterContentInit, Component, EventEmitter, Input, OnInit, OnChanges, Output, OnDestroy} from '@angular/core';
// import '../../../../assets/camera/js/webVideoCtrl.js';
// TODO webVideoCtrl 插件从全局引入改为camera组件内引入
import {MonitorAccount} from './camera.model';

declare var $: any;
declare var WebVideoCtrl: any;

@Component({
    selector: 'kylin-camera',
    templateUrl: './camera.component.html',
    styleUrls: [
        './camera.component.less'
    ]
})
export class CameraComponent implements OnInit, OnChanges, AfterContentInit, OnDestroy {

    @Output() loadingEmit = new EventEmitter<any>();
    @Input() downloadUrl = ''; // 用于下载海康视频插件的api的url
    // @Input() cameraGroupListChange: EventEmitter<any>;
    isInstalled = true; // 海康视频插件是否已安装
    channelList: Array<any> = []; // 通道列表
    @Input() cameraGroupList = new Array<MonitorAccount>(); // 所有摄像头列表，多个
    _dialog = false;
    _playback = false;
    doPlayBack = false;
    g_bPTZAuto = true; // TODO 加备注
    g_iWndIndex: any;
    startDate: any;
    endDate: any;
    loginRecords = []; // 记录已经登录过一次的ip和端口
    loading = false;

    constructor() {
    }

    ngOnInit() {
        // 1- 初始化运行环境：插件监测、插入插件
        this.initCameraEnvironment();
        setTimeout(() => {
            // alert('do playAllCamera fun--------');
            this.playAllCamera();
        }, 1000);

    }

    playAllCamera() {
        // 1- 初始化运行环境：插件监测、插入插件
        // this.initCameraEnvironment();
        // 2- 使用cameraGroupList中的每一条信息登录
        this.loading = true;
        this.loadingEmit.emit(true);
        this.monitorAccountLogin().then(() => {
            // 3- 获取通道，默认获取cameraGroupList中的第一个的通道
            this.channelList = [];
            this.getChannelInfo().then(() => {
                // 4- 开始预览，显示画面
                this.startRealPlay();
            });
        });
    }

    ngOnChanges(data) {
        /*if (data.cameraGroupList && data.cameraGroupList.currentValue.length > 0) {
            // alert('do playAllCamera');
            setTimeout(() => {
                alert('do playAllCamera fun--------');
                this.playAllCamera();
            }, 1000);
        }*/
        if (data.cameraGroupList && data.cameraGroupList.currentValue.length > 0 && !data.cameraGroupList.firstChange) {
            this.playAllCamera();
            alert('do playAllCamera');
        }
    }

    initCameraEnvironment() { // 初始化摄像头配置
        if (-1 === WebVideoCtrl.I_CheckPluginInstall()) { // 检查插件是否已经安装过
            this.isInstalled = false;
            alert('您还未安装过插件，双击开发包目录里的WebComponents.exe安装！');
            return;
        }
        $('.video_nav').addClass('show');
        const video_width = $('.video_nav').width(),
            video_height = $('.video_nav').height();
        // 初始化插件参数及插入插件
        WebVideoCtrl.I_InitPlugin(video_width, video_height, {
            iWndowType: 2,
            // szColorProperty: 'plugin-background:000000;sub-background:000000;sub-border:ffff00;sub-border-select:000000',
            bDebugMode: true,
            cbSelWnd: (xmlDoc) => {
                // TODO 视频打开后的回调
                // 点击选中一个窗口后触发
                this.g_iWndIndex = $(xmlDoc).find('SelectWnd').eq(0).text(); // 当前选中的窗口
                const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
                if (oWndInfo === null) {
                    $('#startRealPlay').removeClass('icon-zanting').addClass('icon-player-play');
                    $('#startPlayBack').removeClass('icon-zanting').addClass('icon-player-play');
                    $('#showSound').removeClass('icon-yinliang').addClass('icon-yinliangjingyin');
                } else {
                    if (oWndInfo.bSound) {
                        $('#showSound').removeClass('icon-yinliangjingyin').addClass('icon-yinliang');
                    } else {
                        $('#showSound').removeClass('icon-yinliang').addClass('icon-yinliangjingyin');
                    }
                    $('#startRealPlay').removeClass('icon-player-play').addClass('icon-zanting');
                    $('#startPlayBack').removeClass('icon-player-play').addClass('icon-zanting');
                }
            }
        });
        // 插入插件到 #divPlugin
        WebVideoCtrl.I_InsertOBJECTPlugin('divPlugin');
        // 检查插件是否最新
        if (-1 === WebVideoCtrl.I_CheckPluginVersion()) {
            alert('检测到新的插件版本，双击开发包目录里的WebComponents.exe升级！');
            return;
        }
        $(window).bind({
            resize: function () {
                const $Restart = $('#restartDiv');
                if ($Restart.length > 0) {
                    const oSize = this.getWindowSize();
                    $Restart.css({
                        width: oSize.width + 'px',
                        height: oSize.height + 'px'
                    });
                }
            }
        });
        $(document.body).bind({
            mouseup: function () {
                setTimeout(function () {
                    // 鼠标点击下拉框后，显示video_frame。保证下拉框显示在frame之上
                    if ($('#vide_sel .dropdown-content').length === 0) {
                        return;
                    }
                    const flag = $('#vide_sel .dropdown-content').css('display');
                    const _heightU = $('#vide_sel .dropdown-content').height() + 30;

                    $('.video_frame').css('height', _heightU);
                    $('.video_frame').css('display', flag);
                }, 0);
            }
        });
        $(window).bind({
            scroll: function () {
                const _winheight = $(window).scrollTop();
            }
        });
    }

    ngAfterContentInit() {
        if ($('#dvTipBox').children().length === 0) {
            $('#vide_sel').remove();
        }
    }

    // 获取窗口尺寸
    getWindowSize() {
        const nWidth = $(this).width() + $(this).scrollLeft(),
            nHeight = $(this).height() + $(this).scrollTop();
        return {width: nWidth, height: nHeight};
    }

    monitorAccountLogin() {
        const promiseArray = new Array<Promise<any>>();
        this.cameraGroupList.forEach(item => {
            promiseArray.push(new Promise((resolve, reject) => {
                // 1 代表主码流(高清输出线路) 2代表子码流(较模糊输出线路)
                /*const rst = this.loginRecords.some(channel => {
                    return channel.ip === item.ip && channel.port === item.port;
                });
                if (rst) {
                    // 校验之前是否登录，如果重复登录会导致I_Login不能触发正常回调函数 success 或者 error
                    resolve();
                    return;
                }*/
                setTimeout(() => {
                    WebVideoCtrl.I_Login(item.ip, 1, item.port, item.username, item.password, {
                        async: true,
                        success: (xmlDoc) => {
                            this.loginRecords.push({ip: item.ip, port: item.port});
                            resolve();
                            // alert('成功1次');
                            console.log('成功1次');
                        },
                        error: (error) => {
                            reject();
                            console.log('失败1次');
                        }
                    });
                }, 1000);
            }));
        });
        return Promise.all(promiseArray);
    }

    startRealPlay() {
        // alert('预览' + this.channelList.length);
        console.log('预览' + this.channelList.length);
        let iRet = 0; // 预览操作是否成功，0成功，-1失败
        // this.channelList = this.channelList.slice(0, 6);
        this.channelList.map((item, index) => {
            if (WebVideoCtrl.I_GetWindowStatus(index) !== null) {
                WebVideoCtrl.I_Stop(index);
                $('#startRealPlay').removeClass('icon-zanting').addClass('icon-player-play');
                // return;
            }
            iRet += WebVideoCtrl.I_StartRealPlay(item.ip, {
                iWndIndex: index,
                iStreamType: 1,
                iChannelID: item.id,
                bZeroChannel: item.bZero
            });
        });
        if (0 === iRet) {
            this.loading = false;
            this.loadingEmit.emit(false);
            this.doPlayBack = true;
            $('#startPlayBack').removeClass('icon-player-play').addClass('icon-zanting');
        }
    }

    // 停止预览
    stopRealPlay() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        const szInfo = '';

        if (oWndInfo != null) {
            const iRet = WebVideoCtrl.I_Stop();
            if (0 === iRet) {
                this.doPlayBack = false;
                $('#startPlayBack').removeClass('icon-zanting').addClass('icon-player-play');
            } else {
                alert('停止预览失败！');
            }
        }
    }

    getChannelInfo() {
        // 获取选中的选中的监控账户列表的通道
        const that = this;
        this.cameraGroupList.map(item => {
            // 模拟通道
            WebVideoCtrl.I_GetAnalogChannelInfo(item.ip, {
                async: true,
                success: function (xmlDoc) {
                    const oChannels = $(xmlDoc).find('VideoInputChannel');
                    $.each(oChannels, function (i) {
                        const id = parseInt($(this).find('id').eq(0).text(), 10);
                        let name = $(this).find('name').eq(0).text();
                        if ('' === name) {
                            name = 'Camera ' + (id < 9 ? '0' + id : id);
                        }
                        const channelItem: any = {
                            ip: item.ip,
                            id,
                            name,
                            bZero: false
                        };
                        that.channelList.push(channelItem);
                    });
                    // alert(item.ip + ' 获取模拟通道成功！');
                },
                error: function () {
                    // alert(item.ip + ' 获取模拟通道失败！');
                }
            });
            // 数字通道
            WebVideoCtrl.I_GetDigitalChannelInfo(item.ip, {
                async: true,
                success: function (xmlDoc) {
                    const oChannels = $(xmlDoc).find('InputProxyChannelStatus');

                    $.each(oChannels, function (i) {
                        const id = parseInt($(this).find('id').eq(0).text(), 10);
                        let name = $(this).find('name').eq(0).text();
                        const online = $(this).find('online').eq(0).text();
                        if ('false' === online) {// 过滤禁用的数字通道
                            return true;
                        }
                        if ('' === name) {
                            const channelLen = this.channelList.length;
                            name = 'IPCamera ' + ((id - channelLen) < 9 ? '0' + (id - channelLen) : (id - channelLen));
                        }
                        const channelItem: any = {
                            ip: item.ip,
                            id,
                            name,
                            bZero: false
                        };
                        that.channelList.push(channelItem);
                    });
                    // alert(item.ip + ' 获取数字通道成功！');
                },
                error: () => {
                    // alert(item.ip + ' 获取数字通道失败！');
                }
            });
            // 零通道
            WebVideoCtrl.I_GetZeroChannelInfo(item.ip, {
                async: true,
                success: function (xmlDoc) {
                    const oChannels = $(xmlDoc).find('ZeroVideoChannel');

                    $.each(oChannels, function (i) {
                        const id = parseInt($(this).find('id').eq(0).text(), 10);
                        let name = $(this).find('name').eq(0).text();
                        if ('' === name) {
                            name = 'Zero Channel ' + (id < 9 ? '0' + id : id);
                        }
                        if ('true' === $(this).find('enabled').eq(0).text()) {// 过滤禁用的零通道
                            const channelItem: any = {
                                ip: item.ip,
                                id,
                                name,
                                bZero: true
                            };
                            that.channelList.push(channelItem);
                        }
                    });
                    // alert(item.ip + ' 获取零通道成功！');
                },
                error: function () {
                    // alert(item.ip + ' 获取零通道失败！');
                }
            });
        });
        // 重新设置窗口分割数
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                this.changeWndNum(Math.ceil(Math.sqrt(this.channelList.length)));
                setTimeout(() => {
                    resolve();
                }, 1000);
            }, 1000);
        });
    }

    // 开始预览
    clickStartPalyAndPalyBack() {
        // this.doPlayBack = false;
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        // const szIP = this.selectIp;
        if (!oWndInfo) {
            return;
        }
        const szIP = oWndInfo.szIP;
        const iStreamType = 1;
        const iChannelID = oWndInfo.iChannelID;
        const bZeroChannel = false;
        const constszInfo = '';
        if ('' === szIP) {
            return;
        }
        if (oWndInfo != null) {// 已经在播放了，停止返回
            WebVideoCtrl.I_Stop();
            $('#startRealPlay').removeClass('icon-zanting').addClass('icon-player-play');
            // return;
        }
        const iRet = WebVideoCtrl.I_StartRealPlay(szIP, {
            iStreamType: iStreamType,
            iChannelID: iChannelID,
            bZeroChannel: bZeroChannel,
        });
        if (0 === iRet) {
            this.doPlayBack = false;
            $('#startRealPlay').removeClass('icon-player-play').addClass('icon-zanting');
            // $('#startPlayBack').removeClass('icon-player-play').addClass('icon-zanting');
        }
    }

    // 窗口分割数
    changeWndNum(iType: number) {
        iType = parseInt(iType + '', 10);
        WebVideoCtrl.I_ChangeWndNum(iType);
    }

    // 云台控制
    mouseDownPTZControl(iPTZIndex) {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex),
            bZeroChannel = false;
        let iPTZSpeed = 4,
            bStop = false;
        if (bZeroChannel) {// 零通道不支持云台
            return;
        }
        if (oWndInfo != null) {
            if (9 === iPTZIndex && this.g_bPTZAuto) {
                iPTZSpeed = 0; // 自动开启后，速度置为0可以关闭自动
                bStop = true;
            } else {
                this.g_bPTZAuto = false; // 点击其他方向，自动肯定会被关闭
                bStop = false;
            }

            WebVideoCtrl.I_PTZControl(iPTZIndex, bStop, {
                iPTZSpeed: iPTZSpeed,
                success: function (xmlDoc) {
                    if (9 === iPTZIndex) {
                        this.g_bPTZAuto = !this.g_bPTZAuto;
                    }
                },
                error: function () {
                }
            });
        }
    }

    // 云台方向停止键
    mouseUpPTZControl() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(1, true, {
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向上调整焦距
    PTZZoomIn() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(10, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向下调整焦距
    PTZZoomout() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(11, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 关闭调整焦距
    PTZZoomStop() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(11, true, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向上调整聚焦
    PTZFocusIn() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(12, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向下调整聚焦
    PTZFoucusOut() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(13, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 关闭调整聚焦
    PTZFoucusStop() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(12, true, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向上调整光圈
    PTZIrisIn() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);

        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(14, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 向下调整光圈
    PTZIrisOut() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(15, false, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 关闭调整光圈
    PTZIrisStop() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        if (oWndInfo != null) {
            WebVideoCtrl.I_PTZControl(14, true, {
                iWndIndex: this.g_iWndIndex,
                success: function (xmlDoc) {
                },
                error: function () {
                }
            });
        }
    }

    // 全屏
    clickFullScreen() {
        WebVideoCtrl.I_FullScreen(true);
    }

    //  抓拍
    fear() {
        this._dialog = true;
    }

    close_dialog() {
        this._dialog = false;
        this._playback = false;
        /*this.startDate = null;
        this.endDate = null;*/
        this.startDate = '';
        this.endDate = '';
    }

    // 抓图
    clickCapturePic() {
        this.clickOpenFileDlg('caPtures', 0);
        const path = $('#caPtures').val();
        if (path !== '' && path !== null) {
            this.clickSetLocalCfg();
            const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
            const szInfo = '';
            if (oWndInfo != null) {
                const szChannelID = '1',
                    szPicName = oWndInfo.szIP.substr(0, oWndInfo.szIP.indexOf(':')) + '_' + szChannelID + '_' + new Date().getTime(),
                    iRet = WebVideoCtrl.I_CapturePic(szPicName);
                console.log(szPicName);
                if (0 === iRet) {
                    alert('图片保存在' + $('#caPtures').val());
                    $('#caPtures').val('');
                } else {
                    alert('抓图失败！');
                }
            }
        }
    }

    // 摄像头录像
    clickStartRecord() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        let iRet = -1;
        let szInfo = '';
        if (!$('#iconcamera').hasClass('icon-iconcamera2')) {
            this.clickOpenFileDlg('caVideos', 0);
            const path = $('#caVideos').val();
            if (path !== '' && path !== null) {
                this.clickSetLocalCfg();
                if (oWndInfo != null) {
                    const szChannelID = oWndInfo.iChannelID,
                        szFileName = oWndInfo.szIP.substr(0, oWndInfo.szIP.indexOf(':')) + '_' + szChannelID + '_' + new Date().getTime();
                    iRet = WebVideoCtrl.I_StartRecord(szFileName);
                    if (iRet === 0) {
                        $('#iconcamera').addClass('icon-iconcamera2');
                    }
                }
            } else {
                return false;
            }
        } else {
            iRet = WebVideoCtrl.I_StopRecord();
            if (0 === iRet) {
                $('#iconcamera').removeClass('icon-iconcamera2');
                alert('录像保存在' + $('#caVideos').val());
                $('#caVideos').val('');
            } else {
                szInfo = '停止录像失败！';
                alert(szInfo);
            }
        }
    }

    // 打开选择框 0：文件夹  1：文件
    clickOpenFileDlg(id, iType) {
        const szDirPath = WebVideoCtrl.I_OpenFileDlg(iType);
        if (szDirPath !== -1 && szDirPath !== '' && szDirPath != null) {
            $('#' + id).val(szDirPath);
        }
    }

    // 获取本地参数
    clickGetLocalCfg() {
        const xmlDoc = WebVideoCtrl.I_GetLocalCfg();
        $('#caPtures').val($(xmlDoc).find('CapturePath').eq(0).text());
        $('#caVideos').val($(xmlDoc).find('RecordPath').eq(0).text());
    }

    // 设置本地参数
    clickSetLocalCfg() {
        const arrXml = [],
            szInfo = '';
        arrXml.push('<LocalConfigInfo>');
        arrXml.push('<CapturePath>' + $('#caPtures').val() + '</CapturePath>');
        arrXml.push('<RecordPath>' + $('#caVideos').val() + '</RecordPath>');
        arrXml.push('<PlaybackPicPath>' + $('#caPtures').val() + '</PlaybackPicPath>');
        arrXml.push('<PlaybackFilePath>' + $('#caVideos').val() + '</PlaybackFilePath>');
        arrXml.push('</LocalConfigInfo>');
        const iRet = WebVideoCtrl.I_SetLocalCfg(arrXml.join(''));
        if (0 !== iRet) {
            alert('本地配置设置失败！');
        }
    }

    // 打开声音
    clickOpenOrCloseSound() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        let iRet = -1,
            szInfo = '';
        if (oWndInfo != null) {
            // 判断当前窗口是否有声音，有则执行关闭，没有则打开声音
            if (oWndInfo.bSound) {
                iRet = WebVideoCtrl.I_CloseSound(oWndInfo.iIndex);
                if (0 === iRet) {
                    // szInfo = '关闭声音成功！';
                    $('#showSound').removeClass('icon-yinliang').addClass('icon-yinliangjingyin');
                } else {
                    // szInfo = '关闭声音失败！';
                }
                return;
            } else {
                iRet = WebVideoCtrl.I_OpenSound();
                if (0 === iRet) {
                    // szInfo = '打开声音成功！';
                    $('#showSound').removeClass('icon-yinliangjingyin').addClass('icon-yinliang');
                } else {
                    // szInfo = '打开声音失败！';
                }
            }
        }
    }

    // 格式化时间
    dateFormat(oDate, fmt) {
        const o = {
            'M+': oDate.getMonth() + 1, // 月份
            'd+': oDate.getDate(), // 日
            'h+': oDate.getHours(), // 小时
            'm+': oDate.getMinutes(), // 分
            's+': oDate.getSeconds(), // 秒
            'q+': Math.floor((oDate.getMonth() + 3) / 3), // 季度
            'S': oDate.getMilliseconds()// 毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (oDate.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (const k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
            }
        }
        return fmt;
    }

    // 摄像头回放
    clickStartPlayback() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        const szCurTime = this.dateFormat(new Date(), 'yyyy-MM-dd');
        if (!oWndInfo) {
            // TODO 获取窗口信息失败
            this.close_dialog();
            alert('获取窗口信息失败,请重试');
            return;
        }
        const szIP = oWndInfo.szIP;
        const bZeroChannel = false;
        const iChannelID = oWndInfo.iChannelID;
        let iRet = -1;
        let szStartTime = this.startDate,
            szEndTime = this.endDate;
        /*let szStartTime = szCurTime + ' 00:00:00',
         szEndTime = szCurTime + ' 23:59:59';*/
        if (this.startDate !== null) {
            szStartTime = this.dateFormat(this.startDate, 'yyyy-MM-dd hh:mm:ss');
        }
        if (this.endDate !== null) {
            szEndTime = this.dateFormat(this.endDate, 'yyyy-MM-dd hh:mm:ss');
        }
        if ('' === szIP) {
            this._playback = false;
            return;
        }
        if (bZeroChannel) {// 零通道不支持回放
            this._playback = false;
            return;
        }
        if (oWndInfo != null) {// 已经在播放了，先停止
            WebVideoCtrl.I_Stop();
            $('#startPlayBack').removeClass('icon-zanting').addClass('icon-player-play');
        }
        iRet = WebVideoCtrl.I_StartPlayback(szIP, {
            iChannelID: iChannelID,
            szStartTime: szStartTime,
            szEndTime: szEndTime
        });
        if (0 === iRet) {
            this.doPlayBack = true;
            this._playback = false;
            //  $('#startRealPlay').removeClass('icon-player-play').addClass('icon-zanting');
            $('#startPlayBack').removeClass('icon-player-play').addClass('icon-zanting');
            this.startDate = null;
            this.endDate = null;
        } else {
            alert(szIP + '没有录像文件,请检测摄像头相关设备是否正常！');
        }
    }

    // 暂停恢复
    clickPlaybackPause() {
        const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
        const szInfo = '';
        if (oWndInfo != null) {
            if ($('#startPlayBack').hasClass('icon-zanting')) {
                const iRet = WebVideoCtrl.I_Pause();
                if (0 === iRet) {
                    $('#startPlayBack').removeClass('icon-zanting').addClass('icon-player-play');
                } else {
                    alert('暂停失败');
                }
            } else {
                const iRet = WebVideoCtrl.I_Resume();
                $('#startPlayBack').removeClass('icon-player-play').addClass('icon-zanting');
                if (0 === iRet) {
                } else {
                    alert('恢复失败');
                }
            }
        }
    }

//  回放
    playback() {
        this._playback = true;
    }

    // 下载海康插件
    downLoadWeb() {
        window.location.href = this.downloadUrl;
    }

    // --------外部插入元素事件响应-开始-----------
    startDateChange(startDate) {
        this.startDate = startDate;
    }

    endDateChange(endDate) {
        this.endDate = endDate;
    }

    // --------外部插入元素事件响应-结束-----------

    ngOnDestroy() {
        this.loginRecords.map(item => {
            WebVideoCtrl.I_Logout(item.ip);
        });
    }
}
